package com.easyboot.web.system.controller;

import com.easyboot.framework.domain.ResultAdaptDto;
import com.easyboot.framework.domain.ZtreeDto;
import com.easyboot.framework.core.ActionResult;
import com.easyboot.framework.core.BaseController;
import com.easyboot.framework.core.RequestHelper;
import com.easyboot.web.system.domain.SysDept;
import com.easyboot.web.system.service.SysDeptService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/25 15:42
 **/
@RequestMapping("/admin/dept")
@Controller
public class SysDeptController extends BaseController {

    @Autowired
    private RequestHelper requestHelper;

    @Autowired
    private SysDeptService deptService;


    /**
    *
     * @author yushuo
     * @description 列表
     * @date 15:43 2021/2/25
     * @param []
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions("system:dept:view")
    @GetMapping("/index")
    public ActionResult index(){
         return view();
    }

    /**
    *
     * @author yushuo
     * @description 部门数据
     * @date 15:45 2021/2/25
     * @param []
     * @return java.util.List<com.easyboot.web.system.domain.SysDept>
    **/
    @RequiresPermissions("system:dept:view")
    @GetMapping("/getDeptTree")
    @ResponseBody
    public List<SysDept> getDeptTree(){
        String deptName=requestHelper.getString("deptName","");
         List<SysDept> list=deptService.getAllDepts(deptName);
        return list;
    }


    @RequiresPermissions(value =  {"system:dept:add","system:dept:edit"},logical = Logical.OR)
    @GetMapping("/form")
    public ActionResult form(String id, Model model){

           model.addAttribute("id",id);
           return view();
    }

    /**
    *
     * @author yushuo
     * @description 获取实体
     * @date 13:42 2021/2/26
     * @param [id]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions(value =  {"system:dept:add","system:dept:edit"},logical = Logical.OR)
    @GetMapping("/getModel")
    public ActionResult getModel(Long id){
        ResultAdaptDto ret=new ResultAdaptDto();
        if(id>0){
             SysDept dept=deptService.getModel(id);
             ret.put("model",dept);
        }
        List<ZtreeDto> data= deptService.getDeptZtree();

        ret.put("ztreeData",data);

        return json(ret);
    }


    /**
    *
     * @author yushuo
     * @description 保存
     * @date 14:15 2021/2/26
     * @param [dto]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions(value =  {"system:dept:add","system:dept:edit"},logical = Logical.OR)
    @PostMapping("/save")
     public ActionResult save(SysDept dto){

        deptService.save(dto, requestHelper.getUserOwnInfo());
        return ok("部门数据已保存");
     }


    /**
    *
     * @author yushuo
     * @description 获取部门下来树形菜单
     * @date 14:15 2021/2/26
     * @param []
     * @return com.easyboot.framework.web.ActionResult
    **/
    @GetMapping("/getDeptZtree")
    public ActionResult getDeptZtree(){
        ResultAdaptDto ret=new ResultAdaptDto();
        List<ZtreeDto> data= deptService.getDeptZtree();

        ret.put("ztreeData",data);
        return json(ret);
    }

    @RequiresPermissions("system:dept:delete")
    @GetMapping("/delete")
    public ActionResult delete(Long id){
        if(id==0){
            return error("删除数据失败");
        }
        ResultAdaptDto ret= deptService.delete(id);
        return json(ret);
    }
}
